<html>
<head><meta charset="utf-8"><title>Common misconception: `const fn` and its effect on codegen · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html">Common misconception: `const fn` and its effect on codegen</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="208059113"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208059113" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208059113">(Aug 26 2020 at 07:12)</a>:</h4>
<p>At least once a month, somewhere on Zulip or GitHub someone claims that making a function <code>const</code> will somehow make it evaluated at compile time as part of the optimizer.  Typically this is not even stated as a hypothesis but as a fact -- somehow people are convinced that this is true, even though it isn't, it never was, and AFAIK it also is not true in any other language. <code>const fn</code> and <code>fn</code> are compiled exactly the same way. <code>const</code>ness of a <code>fn</code> has no effect at all on the optimizer. The <em>only</em> effect <code>const</code> has on a <code>fn</code> is that you may now call it in more places, namely in <code>const</code>/<code>static</code> initializers. But every program that compiles without the <code>const</code>, is unaffected by the <code>const</code>.</p>
<p>Any idea where this common misconception is coming from? Are there docs that are ambiguous, or compiler messages, or so? Explaining this over and over clearly doesn't help.^^<br>
Cc <span class="user-group-mention" data-user-group-id="1916">@WG-const-eval</span></p>



<a name="208060073"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208060073" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208060073">(Aug 26 2020 at 07:26)</a>:</h4>
<p>Is there a way to force const-eval? If I have <code>const fn foo(n: u8) -&gt; u32</code> and I want <code>let x = foo(42);</code> to be turned into a constant assignment, do I have to write <code>const x_const = foo(42); let x = x_const;</code>?</p>



<a name="208060760"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208060760" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208060760">(Aug 26 2020 at 07:35)</a>:</h4>
<p>cc <a href="https://github.com/rust-lang/rust/pull/75894#discussion_r476480546">https://github.com/rust-lang/rust/pull/75894#discussion_r476480546</a> where I stated that. I'm sorry if I claim it wrongly.</p>



<a name="208061421"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208061421" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208061421">(Aug 26 2020 at 07:44)</a>:</h4>
<p><span class="user-mention" data-user-id="271719">@Mario Carneiro</span> are you asking to force const propagataion? const evaluation is never optional. so "forcing" just doesnt make sense as a concept here.</p>



<a name="208061437"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208061437" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208061437">(Aug 26 2020 at 07:44)</a>:</h4>
<p><span class="user-mention" data-user-id="209117">@lzutao</span> it's okay, you're by far not the only one :)</p>



<a name="208061471"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208061471" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208061471">(Aug 26 2020 at 07:45)</a>:</h4>
<p>but if you have any idea why you thought this was the case, that might be useful for preventing this misconception in the future</p>



<a name="208061505"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208061505" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208061505">(Aug 26 2020 at 07:45)</a>:</h4>
<p>In a context like <code>const FOO = foo(42);</code> obviously rustc has to evaluate the function at compile time, but in <code>let x = foo(42);</code> it might put a call to <code>foo</code> there</p>



<a name="208061524"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208061524" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208061524">(Aug 26 2020 at 07:45)</a>:</h4>
<p>the bodies of <code>const</code> and <code>static</code> items are computed at compile time. that is called const evaluation.</p>



<a name="208061587"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208061587" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208061587">(Aug 26 2020 at 07:46)</a>:</h4>
<p>also, and this has nothing to do with const evaluation, the compiler will replace <code>2+2</code> by <code>4</code> in your code. this is an optimization called const <em>propagation</em>.</p>



<a name="208061620"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208061620" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208061620">(Aug 26 2020 at 07:46)</a>:</h4>
<p>I mean, looking at the optimized LLVM IR or x86 asm, I may make that claim about optimizing <code>const fn</code>.</p>



<a name="208061636"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208061636" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208061636">(Aug 26 2020 at 07:47)</a>:</h4>
<p><span class="user-mention silent" data-user-id="209117">lzutao</span> <a href="#narrow/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen/near/208061620">said</a>:</p>
<blockquote>
<p>I mean, looking at the optimized LLVM IR or x86 asm, I may make that claim about optimizing <code>const fn</code>.</p>
</blockquote>
<p>but the <code>const</code> has no effect on the LLVM IR. the program generates the exact same IR without <code>const</code>.</p>



<a name="208061668"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208061668" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208061668">(Aug 26 2020 at 07:47)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen/near/208061505">said</a>:</p>
<blockquote>
<p>In a context like <code>const FOO = foo(42);</code> obviously rustc has to evaluate the function at compile time, but in <code>let x = foo(42);</code> it might put a call to <code>foo</code> there</p>
</blockquote>
<p>right. the former is const evaluaton, the latter is const propagation. they have nothing to do with each other and happen in totally different phases of compilation.</p>



<a name="208061691"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208061691" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208061691">(Aug 26 2020 at 07:48)</a>:</h4>
<p>yeah, you're right about IR. Still I don't see the call of <code>foo(42)</code> in x86 asm.</p>



<a name="208061789"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208061789" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208061789">(Aug 26 2020 at 07:49)</a>:</h4>
<p><span class="user-mention" data-user-id="271719">@Mario Carneiro</span> note that you might also have <code>let x = foo(some_runtime_value);</code>, even when <code>foo</code> is <code>const fn</code>. at that point clearly it is impossible to remove the <code>foo</code> call entirely (we can inline but it might not help much). Being <code>const fn</code> does not mean that all calls to this functions only contain consts!</p>



<a name="208061797"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208061797" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208061797">(Aug 26 2020 at 07:49)</a>:</h4>
<p>right</p>



<a name="208061800"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208061800" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208061800">(Aug 26 2020 at 07:49)</a>:</h4>
<p><span class="user-mention silent" data-user-id="209117">lzutao</span> <a href="#narrow/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen/near/208061691">said</a>:</p>
<blockquote>
<p>yeah, you're right about IR. Still I don't see the call of <code>foo(42)</code> in x86 asm.</p>
</blockquote>
<p>yeah, that's called the "optimizer" which tries to make your program go fast. ;) It has nothing to do with <code>const</code>/<code>static</code>/<code>const fn</code>.</p>



<a name="208061832"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208061832" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208061832">(Aug 26 2020 at 07:49)</a>:</h4>
<p>I'm just wondering if there is some way to say <code>let x = (const foo(42))</code> or something and be sure that it got inlined</p>



<a name="208061890"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208061890" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208061890">(Aug 26 2020 at 07:50)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen/near/208061832">said</a>:</p>
<blockquote>
<p>I'm just wondering if there is some way to say <code>let x = (const foo(42))</code> or something and be sure that it got inlined</p>
</blockquote>
<div class="codehilite"><pre><span></span><code><span class="k">const</span><span class="w"> </span><span class="n">FOO</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">foo</span><span class="p">(</span><span class="mi">32</span><span class="p">);</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">FOO</span><span class="p">;</span><span class="w"></span>
</code></pre></div>



<a name="208061903"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208061903" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208061903">(Aug 26 2020 at 07:50)</a>:</h4>
<p>the only <em>guaranteed</em> compile-time evaluated code is const and static initializers</p>



<a name="208061916"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208061916" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208061916">(Aug 26 2020 at 07:50)</a>:</h4>
<p>there's an RFC to make such cases a bit easier to write: <a href="https://github.com/rust-lang/rfcs/pull/2920">https://github.com/rust-lang/rfcs/pull/2920</a></p>



<a name="208061917"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208061917" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208061917">(Aug 26 2020 at 07:50)</a>:</h4>
<p>that's what I mean the misconception comes from. Mostly are from C/C++ devs like me who want to explore how fast Rust is.<br>
They inspect the asm and claim that.</p>



<a name="208061937"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208061937" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208061937">(Aug 26 2020 at 07:51)</a>:</h4>
<p><span class="user-mention silent" data-user-id="209117">lzutao</span> <a href="#narrow/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen/near/208061917">said</a>:</p>
<blockquote>
<p>that's what I mean the misconception comes from. Mostly are from C/C++ devs like me who want to explore how fast Rust is.<br>
They inspect the asm and claim that.</p>
</blockquote>
<p>but C/C++ do the exact same thing as Rust here</p>



<a name="208061964"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208061964" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208061964">(Aug 26 2020 at 07:51)</a>:</h4>
<p>in C++ you also have functions being inlined and const-propagated without writing <code>constexpr</code> anywhere</p>



<a name="208061965"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208061965" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208061965">(Aug 26 2020 at 07:51)</a>:</h4>
<p>do you think it would be a bad idea to infer constness of constants and eagerly evaluate everything that can be?</p>



<a name="208061992"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208061992" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208061992">(Aug 26 2020 at 07:51)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen/near/208061965">said</a>:</p>
<blockquote>
<p>do you think it would be a bad idea to infer constness of constants and eagerly evaluate everything that can be?</p>
</blockquote>
<p>to me that sounds like a weird description of const propagation</p>



<a name="208062039"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208062039" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208062039">(Aug 26 2020 at 07:52)</a>:</h4>
<p>that is, infer that since <code>foo(42)</code> is valid in const context, it should always be const eval'd</p>



<a name="208062054"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208062054" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208062054">(Aug 26 2020 at 07:52)</a>:</h4>
<p>it is const propagation</p>



<a name="208062069"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208062069" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208062069">(Aug 26 2020 at 07:52)</a>:</h4>
<p>no that doesnt make much sense IMO. "valid in const context" is something very different from "can be propagated by the compiler".</p>



<a name="208062134"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208062134" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208062134">(Aug 26 2020 at 07:53)</a>:</h4>
<p>for example const-prop can exploit the fact that if <code>ptr: &amp;i32</code>, then <code>let x = ptr as *const _ as usize</code> is always even, so <code>x % 2</code> is 0. thenw e can propgatae that 0 further.</p>



<a name="208062148"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208062148" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208062148">(Aug 26 2020 at 07:53)</a>:</h4>
<p>it's a bit weaker than const propagation in general (since that can handle <code>x * 0 ~&gt; 0</code> things), but also probably a bit stronger because it will evaluate functions even if the inliner thinks it might not be such a good idea</p>



<a name="208062155"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208062155" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208062155">(Aug 26 2020 at 07:53)</a>:</h4>
<p>this is clearly not valid in const context though as we do not know the value of <code>ptr</code>.</p>



<a name="208062247"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208062247" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208062247">(Aug 26 2020 at 07:54)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen/near/208062148">said</a>:</p>
<blockquote>
<p>it's a bit weaker than const propagation in general (since that can handle <code>x * 0 ~&gt; 0</code> things), but also probably a bit stronger because it will evaluate functions even if the inliner thinks it might not be such a good idea</p>
</blockquote>
<p>fair. one could play with heuristics like that. there might be useful things to do there. but it requires careful design and hasn't happened yet, so while it could hyptoehtically help, it doesnt answer the question for why people claim <code>const</code> <em>already</em> has effects like that.</p>



<a name="208062297"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208062297" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208062297">(Aug 26 2020 at 07:55)</a>:</h4>
<p>one big problem with doing this naively is that the call <code>foo(42)</code> might panic or loop forever or so, and then we have to bail out and not touch it or we get <a href="https://github.com/rust-lang/const-eval/issues/53">a huge amount of problems</a>.</p>



<a name="208062494"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208062494" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208062494">(Aug 26 2020 at 07:57)</a>:</h4>
<p>but I didn't want to discuss here how <code>const</code> might help guide runtime optimizations of code, I was asking why people think that it already somehow does this even though it doesn't. maybe people just look at assembly and didnt set up their experiments correctly and take wrong conclusions? like only testing with <code>const</code> and not without and thus not realizing that the <code>const</code> has no effect...</p>



<a name="208063113"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208063113" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208063113">(Aug 26 2020 at 08:05)</a>:</h4>
<p>It is a natural assumption. "<code>constexpr</code> all the things" to get more CTFE is the big selling point in the first place. Without knowing how it is implemented, I would have given even odds on the behavior I described vs the behavior you are indicating. I wonder how much of peoples' impressions of the compiler are based on what they think it would be logical for it to do compared to what they have actually seen it do? <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span></p>



<a name="208067620"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208067620" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208067620">(Aug 26 2020 at 09:04)</a>:</h4>
<p>My guess is that they look at ASM, see that it was const-prop'd, and misunderstand that as being const-evaluated.</p>
<p>I hope <code>const{}</code> will make this easier to talk about.</p>



<a name="208071545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208071545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208071545">(Aug 26 2020 at 09:50)</a>:</h4>
<p><span class="user-mention" data-user-id="271719">@Mario Carneiro</span> but the thing is, inlining and constprop happened in C++ way before it had <code>constexpr</code>. There's nothing new here when <code>foo(42)</code> disappears in the assembly. I really don't understand how attributing this to <code>const(expr)</code> is a "natural assumption".</p>
<p>My guess is people set up a flawed experiment, like trying something with <code>constexpr</code> and seeing it const-propped, and forget to remove the <code>cosntexpr</code> to actually do a meaningful experiment of what <code>constexpr</code> does (not) do</p>



<a name="208071588"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208071588" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208071588">(Aug 26 2020 at 09:51)</a>:</h4>
<p>And AFAIK not even C++ compilers use <code>constexpr</code> as an optimization hint, which to me says it is hard to get any meaningful speedup out of such a heuristic for real code. It seems logical on first sight to treat <code>const_fn(42)</code> specially but I am not convinced it is really that useful in practice.</p>



<a name="208071840"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208071840" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208071840">(Aug 26 2020 at 09:54)</a>:</h4>
<p>I wonder if it's worth writing a blog post like "CTFE != const-prop"... but then I already discussed this in <a href="https://www.ralfj.de/blog/2018/07/19/const.html">https://www.ralfj.de/blog/2018/07/19/const.html</a> which was widely read at the time, but new people show up all the time ( <span aria-label="tada" class="emoji emoji-1f389" role="img" title="tada">:tada:</span> ) and of course they don't know what it says in old blog posts.</p>



<a name="208071850"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208071850" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208071850">(Aug 26 2020 at 09:54)</a>:</h4>
<p>that's why I was wondering if there's a good place in the docs to clarify this.</p>



<a name="208071941"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208071941" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208071941">(Aug 26 2020 at 09:55)</a>:</h4>
<p><code>const</code> keyword docs in the stdlib docs plus a <code>Note:</code> in the reference.</p>



<a name="208072070"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208072070" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208072070">(Aug 26 2020 at 09:57)</a>:</h4>
<p>We don't really have a good place to talk about the optimizations that the compiler does in practice.</p>



<a name="208072242"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208072242" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Poliorcetics <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208072242">(Aug 26 2020 at 09:59)</a>:</h4>
<p>If the doc must be changed for the const keyword and you don’t have the time ping me in the issue, I’ll do it <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span></p>



<a name="208077346"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208077346" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pachi <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208077346">(Aug 26 2020 at 10:59)</a>:</h4>
<p>I think people see const expressions as supercharged const propagation</p>



<a name="208096797"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208096797" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208096797">(Aug 26 2020 at 14:16)</a>:</h4>
<blockquote>
<p>Any idea where this common misconception is coming from? Are there docs that are ambiguous, or compiler messages, or so? Explaining this over and over clearly doesn't help.^^</p>
</blockquote>
<p>I've definitely seen informal explanations of const functions in blog posts, Reddit comments, etc as "code run at compile time". I have no idea how to correct that other than when it pops up.</p>



<a name="208096918"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208096918" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208096918">(Aug 26 2020 at 14:17)</a>:</h4>
<p>Perhaps having a canonical explanation that <code>const</code> does not always mean "run at compile time" would make it easier to correct people as it can just be linked to and updated as appropriate.</p>



<a name="208105683"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208105683" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208105683">(Aug 26 2020 at 15:16)</a>:</h4>
<p>For my own edification, <code>const</code> does mean <em>is guaranteed that this <strong>can</strong> be run at compile time</em>, right? If so, it might be the mix of "guarantee" and "can".</p>



<a name="208105868"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208105868" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208105868">(Aug 26 2020 at 15:17)</a>:</h4>
<p>It's not guaranteed that it will ever <em>succeed</em></p>



<a name="208106029"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208106029" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208106029">(Aug 26 2020 at 15:18)</a>:</h4>
<p>i.e. it might panic, or something more nuanced?</p>



<a name="208106111"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208106111" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208106111">(Aug 26 2020 at 15:19)</a>:</h4>
<p>Panic, infinite loop, infinite recursion</p>



<a name="208106163"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208106163" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208106163">(Aug 26 2020 at 15:19)</a>:</h4>
<p>Yeah, I meant the class of anything that results in <code>-&gt; !</code></p>



<a name="208106667"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208106667" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208106667">(Aug 26 2020 at 15:22)</a>:</h4>
<p>As a side note, I'm a tiny bit amused at "a canonical explanation [...] linked to and updated as appropriate". That's what duplicate marking on Stack Overflow basically is, and a surprising number of people hate you for it.</p>



<a name="208106796"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208106796" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208106796">(Aug 26 2020 at 15:23)</a>:</h4>
<p>But! If someone wants to, Stack Overflow could be a place to put that information (you can self-ask-and-answer questions)</p>



<a name="208120981"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208120981" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208120981">(Aug 26 2020 at 17:17)</a>:</h4>
<p>I must say actually i'm interested in seeing some form of partial evaluation support in Rust...  Though maybe some explicit syntax is needed ... <br>
I imagine it useful to explicitly ask the compiler to auto-generate highly specialized version of a function on request, by lifting some argument to const generic position and perform dead code elimination and other partial evaluation techniques.</p>



<a name="208123190"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208123190" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Poliorcetics <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208123190">(Aug 26 2020 at 17:36)</a>:</h4>
<p><a href="https://reddit.com/r/rust/comments/ih0cip/constany_convert_any_rust_function_to_const/">https://reddit.com/r/rust/comments/ih0cip/constany_convert_any_rust_function_to_const/</a> case in point</p>



<a name="208123218"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208123218" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Poliorcetics <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208123218">(Aug 26 2020 at 17:37)</a>:</h4>
<p>This popped just today, a few hours ago</p>



<a name="208123234"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208123234" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208123234">(Aug 26 2020 at 17:37)</a>:</h4>
<blockquote>
<p>Constany: convert any rust function to const function</p>
</blockquote>
<p>Press <span aria-label="x" class="emoji emoji-274e" role="img" title="x">:x:</span> to doubt</p>



<a name="208123360"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208123360" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208123360">(Aug 26 2020 at 17:38)</a>:</h4>
<blockquote>
<p>the function marked as constany::const_fn and the main function will be compiled twice. For the first time, the value of the function will be recorded. For the second time, the function will be replaced by the value.</p>
</blockquote>
<p>Oh. They are in the camp of not caring about consistency :-)</p>



<a name="208152722"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208152722" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208152722">(Aug 26 2020 at 21:41)</a>:</h4>
<p>I had this misconception as well until I saw this :). IIUC, <code>const x: i32 = some_function()</code> where <code>some_function</code> is a <code>const fn</code> will be evaluated at compile time, but <code>let x: i32 = some_function()</code> will not?</p>



<a name="208153494"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208153494" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> jknodt <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208153494">(Aug 26 2020 at 21:48)</a>:</h4>
<p><code>const x: i32 = some_function()</code> will be guaranteed to be evaluated at compile time, whereas <code>let x = some_function()</code> may be evaluated at compile time, due to optimizations like const-propogation, but might not be, I believe is what RalfJ said earlier:<br>
<span class="user-mention silent" data-user-id="120791">RalfJ</span> <a href="#narrow/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen/near/208061668">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen/near/208061505">said</a>:</p>
<blockquote>
<p>In a context like <code>const FOO = foo(42);</code> obviously rustc has to evaluate the function at compile time, but in <code>let x = foo(42);</code> it might put a call to <code>foo</code> there</p>
</blockquote>
<p>right. the former is const evaluaton, the latter is const propagation. they have nothing to do with each other and happen in totally different phases of compilation.</p>
</blockquote>



<a name="208154943"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208154943" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208154943">(Aug 26 2020 at 22:04)</a>:</h4>
<p>I think the reason it is so misunderstood is because <code>const fn</code> communicates (at least to me) that the function behaves the same as a <code>const FOO</code>; in other words, it's <em>always</em> evaluated at compile time. To me, I think it might be clearer as something like <code>pure fn</code>, but of course it wouldn't make sense to change at this point.</p>



<a name="208156158"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208156158" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208156158">(Aug 26 2020 at 22:17)</a>:</h4>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> <a href="#narrow/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen/near/208071588">said</a>:</p>
<blockquote>
<p>And AFAIK not even C++ compilers use <code>constexpr</code> as an optimization hint, which to me says it is hard to get any meaningful speedup out of such a heuristic for real code. It seems logical on first sight to treat <code>const_fn(42)</code> specially but I am not convinced it is really that useful in practice.</p>
</blockquote>
<p>it looks like sometimes <code>constexpr</code> can even <em>prevent</em> const propagation and inlining under <code>-O1</code></p>



<a name="208185302"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208185302" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208185302">(Aug 27 2020 at 07:26)</a>:</h4>
<p><span class="user-mention silent" data-user-id="307537">Camelid</span> <a href="#narrow/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen/near/208152722">said</a>:</p>
<blockquote>
<p>I had this misconception as well until I saw this :). IIUC, <code>const x: i32 = some_function()</code> where <code>some_function</code> is a <code>const fn</code> will be evaluated at compile time, but <code>let x: i32 = some_function()</code> will not?</p>
</blockquote>
<p>that's how I would put it, yes. as others said, optimziations make this more complicated, but optimizations are not guaranteed to happen and have no observable effect other than making your code go faster.</p>



<a name="208185307"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208185307" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208185307">(Aug 27 2020 at 07:26)</a>:</h4>
<p><span class="user-mention silent" data-user-id="249222">Elichai Turkel</span> <a href="#narrow/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen/near/208156158">said</a>:</p>
<blockquote>
<p>it looks like sometimes <code>constexpr</code> can even <em>prevent</em> const propagation and inlining under <code>-O1</code></p>
</blockquote>
<p>oh wow... why does that happen?</p>



<a name="208185331"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208185331" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208185331">(Aug 27 2020 at 07:27)</a>:</h4>
<p><span class="user-mention silent" data-user-id="307537">Camelid</span> <a href="#narrow/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen/near/208154943">said</a>:</p>
<blockquote>
<p>I think the reason it is so misunderstood is because <code>const fn</code> communicates (at least to me) that the function behaves the same as a <code>const FOO</code>; in other words, it's <em>always</em> evaluated at compile time. To me, I think it might be clearer as something like <code>pure fn</code>, but of course it wouldn't make sense to change at this point.</p>
</blockquote>
<p>but then why are people not surprised that they can call <code>const fn</code> from normal <code>fn</code> with non-const arguments? That would immediately show that it cannot be "always evaluated at compile time".</p>



<a name="208185497"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208185497" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> jknodt <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208185497">(Aug 27 2020 at 07:29)</a>:</h4>
<p>For me, it would be that if I did call it with non-const arguments, I would expect it would need to be called. If I call it with const arguments, I would always expect it to be evaluated at compile time</p>



<a name="208186082"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208186082" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208186082">(Aug 27 2020 at 07:36)</a>:</h4>
<p>yeah no it's nothing like that. the notion of a "const argument" is subtle, and even <em>with</em> const arguments the function can fail (panic, loop forever, ...) so we couln't always do this.</p>



<a name="208186105"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208186105" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208186105">(Aug 27 2020 at 07:37)</a>:</h4>
<p>For cases where we <em>can</em> do it, why would we only do it for <code>const fn</code>? would be a shame to not optimize all the other functions as well.</p>



<a name="208186488"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208186488" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> jknodt <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208186488">(Aug 27 2020 at 07:42)</a>:</h4>
<p>Hm, I mean from the perspective of a user who wrote a specific function which they cannot see cases where it might overflow(arithmetic operations are probably the most common use case for const fns) or panic or loop forever, it seems plausible that that specific function would be evaluated at compile time. Thinking about how other functions are compiled is probably not on their mind as well. I think it's most likely a thing which needs counterexamples to demonstrate behaviour of when it won't be propagated, and also explicit mention that all functions can be const-propagated</p>



<a name="208187194"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208187194" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208187194">(Aug 27 2020 at 07:53)</a>:</h4>
<p><em>Obviously</em> we need a new keyword <code>nofail</code> for these functions that don't diverge. ;)  (Yes, I know why it'd never work)</p>



<a name="208222635"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208222635" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dániel Buga <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208222635">(Aug 27 2020 at 14:19)</a>:</h4>
<p>Sorry, I'm also at fault here. For some reason I associated const with a subset of language features the compiler can - for sure - evaluate and so functions will be as well if their input is known at compile time.</p>
<p>A bit of an off-topic, but what is needed for this to happen? I have some cases in a GH issue, that, from the perspective of a layperson seem like they should be evaluated, but aren't.</p>



<a name="208232190"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208232190" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gerald E. Butler <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208232190">(Aug 27 2020 at 15:22)</a>:</h4>
<p>In theory, could a compiler use the constness of a function to automagically use memoization for complex functions? Aside from whether or not it would be a desirable feature, would the fact that a function is annotated with const be of  any use if we wanted the compiler to perform such an optimization?</p>



<a name="208241303"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208241303" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Poliorcetics <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208241303">(Aug 27 2020 at 16:25)</a>:</h4>
<p><span class="user-mention" data-user-id="130459">@Gerald E. Butler</span> I don’t think it matters, especially as the differences between normal fn and const fn are reduced</p>



<a name="208241475"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208241475" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208241475">(Aug 27 2020 at 16:26)</a>:</h4>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> <a href="#narrow/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen/near/208185307">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="249222">Elichai Turkel</span> <a href="#narrow/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen/near/208156158">said</a>:</p>
<blockquote>
<p>it looks like sometimes <code>constexpr</code> can even <em>prevent</em> const propagation and inlining under <code>-O1</code></p>
</blockquote>
<p>oh wow... why does that happen?</p>
</blockquote>
<p>I see that I've forgot to link to godbolt, here: <a href="https://godbolt.org/z/EWG5bE">https://godbolt.org/z/EWG5bE</a>. don't know why that happens</p>



<a name="208243489"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208243489" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gerald E. Butler <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208243489">(Aug 27 2020 at 16:42)</a>:</h4>
<p><span class="user-mention silent" data-user-id="307289">Poliorcetics</span> <a href="#narrow/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen/near/208241303">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="130459">Gerald E. Butler</span> I don’t think it matters, especially as the differences between normal fn and const fn are reduced</p>
</blockquote>
<p>Are you saying that the compiler can determine the constness of a function independent of the const annotation and so could perform const related optimizations regardless of the annotation and that the annotation is just a commitment by the API author that the fn will always be const (barring a major semver bump)?</p>



<a name="208252423"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208252423" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208252423">(Aug 27 2020 at 17:53)</a>:</h4>
<p><span class="user-mention" data-user-id="249222">@Elichai Turkel</span> interesting -- and GCC does better in the <code>constexpr</code> case, looks inlined</p>



<a name="208252617"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208252617" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208252617">(Aug 27 2020 at 17:55)</a>:</h4>
<p>actually GCC inlines both, but still emits a <code>square</code> function in the non-const case</p>



<a name="208262739"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208262739" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Poliorcetics <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208262739">(Aug 27 2020 at 19:12)</a>:</h4>
<p><span class="user-mention" data-user-id="130459">@Gerald E. Butler</span> a const fn is a commitment by the api author(s) that the function will be usable in const context. Constant propagation and constant evaluation (I think you’re talking about that ?) are separate from that ^^</p>



<a name="208266747"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208266747" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208266747">(Aug 27 2020 at 19:47)</a>:</h4>
<p>Related Update: the RFC for const blocks has been accepted: <a href="https://github.com/rust-lang/rust/issues/76001">https://github.com/rust-lang/rust/issues/76001</a></p>



<a name="208280543"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208280543" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208280543">(Aug 27 2020 at 21:45)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> Apperantly in C++ <code>constexpr</code> is implicitly <code>inline</code>, so it's not exactly "the same as regular function" <br>
<a href="https://twitter.com/shafikyaghmour/status/1299058963447595008/">https://twitter.com/shafikyaghmour/status/1299058963447595008/</a><br>
<a href="http://eel.is/c++draft/dcl.constexpr#1.sentence-3">http://eel.is/c++draft/dcl.constexpr#1.sentence-3</a></p>
<div class="inline-preview-twitter"><div class="twitter-tweet"><a href="https://twitter.com/shafikyaghmour/status/1299058963447595008/"><img class="twitter-avatar" src="https://pbs.twimg.com/profile_images/1200601050694082560/w4NubWzl_normal.jpg"></a><p><a href="https://twitter.com/Elichai2">@Elichai2</a> <a href="https://twitter.com/llvmorg">@llvmorg</a> <a href="https://twitter.com/zygoloid">@zygoloid</a> It has the same behavior as inline, which is consistent since constexpr functions are implicitly inline: <a href="https://t.co/c8pSl3Zu06">http://eel.is/c++draft/dcl.constexpr#1.sentence-3</a>

It applies it during O2: <a href="https://t.co/070LJS4KYJ">https://godbolt.org/z/oE76vn</a>

So it seems a purposeful decision but not sure why. <a href="https://t.co/Qajw255uAg">https://twitter.com/shafikyaghmour/status/1299058963447595008/photo/1</a></p><span>- Shafik Yaghmour (@shafikyaghmour)</span><div class="twitter-image"><a href="https://t.co/Qajw255uAg"><img src="https://pbs.twimg.com/media/EgcvGhJVkAEWEQg.jpg:small"></a></div></div></div>



<a name="208363519"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208363519" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208363519">(Aug 28 2020 at 16:15)</a>:</h4>
<p><span class="user-mention" data-user-id="249222">@Elichai Turkel</span> interesting, I did not know that.</p>



<a name="208363656"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208363656" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208363656">(Aug 28 2020 at 16:16)</a>:</h4>
<p>re: using <code>const</code> as an optimziation hint -- considering that this will still require non-trivial analyses, I think the main open question there is why one would restrict this to <code>const</code> fn. Not all <code>const fn</code> <em>successfully</em> evaluate to a constant that the compiler can just embed; if the compiler can determine that a fn call evaluates to a constant then why only do that for <code>const fn</code>?</p>



<a name="208363761"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208363761" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208363761">(Aug 28 2020 at 16:17)</a>:</h4>
<p>personally I would be worried about losses in compile time</p>



<a name="208363796"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208363796" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208363796">(Aug 28 2020 at 16:17)</a>:</h4>
<p>even if const evaluation works perfectly and does everything right it still takes some amount of time to execute</p>



<a name="208363803"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208363803" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208363803">(Aug 28 2020 at 16:17)</a>:</h4>
<p>as more and more <code>fn</code> become <code>const fn</code>, the same argument applies there</p>



<a name="208363869"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208363869" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208363869">(Aug 28 2020 at 16:18)</a>:</h4>
<p>we'll eventually be able to make almost all of libcore and liballoc <code>const fn</code>. or so we think.</p>



<a name="208363941"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208363941" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208363941">(Aug 28 2020 at 16:18)</a>:</h4>
<p>yes, there are <a href="https://rust-lang.github.io/const-eval/">grand plans</a> ;) but it'll take time</p>



<a name="208365719"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208365719" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208365719">(Aug 28 2020 at 16:34)</a>:</h4>
<p>When will there be a lint that tells you a function can be const?</p>



<a name="208365918"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208365918" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208365918">(Aug 28 2020 at 16:36)</a>:</h4>
<p>That should probably be disabled by default, since it's a semver break to change it back from const to non-const (thus you shouldn't blindly make everything <code>const</code> just because today they're implemented in a <code>const fn</code>-compatible way).</p>



<a name="208366003"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208366003" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208366003">(Aug 28 2020 at 16:36)</a>:</h4>
<p>There already is a lint: <a href="https://rust-lang.github.io/rust-clippy/master/index.html#missing_const_for_fn">https://rust-lang.github.io/rust-clippy/master/index.html#missing_const_for_fn</a></p>



<a name="208369717"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208369717" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208369717">(Aug 28 2020 at 17:06)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116155">Jake Goulding</span> <a href="#narrow/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen/near/208365719">said</a>:</p>
<blockquote>
<p>When will there be a lint that tells you a function can be const?</p>
</blockquote>
<p>isn't the true question: with which edition will <code>const</code> become the default, and non-const operations require opting out? ;)</p>



<a name="208408145"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208408145" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208408145">(Aug 29 2020 at 00:36)</a>:</h4>
<p>And on that day we really are Haskell</p>



<a name="208426524"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208426524" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208426524">(Aug 29 2020 at 09:58)</a>:</h4>
<p><code>const fn</code> will have mutable state, so... not really I think^^</p>



<a name="208433809"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208433809" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208433809">(Aug 29 2020 at 13:23)</a>:</h4>
<p>So does Haskell though</p>



<a name="208438376"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208438376" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208438376">(Aug 29 2020 at 15:09)</a>:</h4>
<p>not in the same way. <code>const fn</code> has mutable state outside monads.</p>



<a name="208439271"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208439271" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pickfire <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208439271">(Aug 29 2020 at 15:29)</a>:</h4>
<p>I am sorry that I was probably the one who caused this post, I never read this in any docs but I thought more power to const means better codegen but looks like it wasn't.</p>



<a name="208439338"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208439338" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pickfire <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208439338">(Aug 29 2020 at 15:30)</a>:</h4>
<p>Still, wouldn't later everyone use <code>const fn</code> rather than <code>fn</code> when possible? I wonder why wouldn't <code>const</code> be opt-in rather than opt-out like <code>mut</code> once it became fully compatible with <code>fn</code> later?</p>



<a name="208440491"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208440491" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208440491">(Aug 29 2020 at 15:55)</a>:</h4>
<p>There are API guarantees to take into account: by using <code>const fn</code> you are promising that future revisions of your API won't change the function to be non-const.</p>



<a name="208461209"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208461209" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pickfire <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208461209">(Aug 30 2020 at 00:33)</a>:</h4>
<p>Wouldn't people usually want <code>const</code> rather than not? Is there any reason why people don't want a function to be <code>const</code>?</p>



<a name="208467728"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208467728" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Manmeet Singh <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208467728">(Aug 30 2020 at 04:20)</a>:</h4>
<p>What is the chance this will be evaluated at compile time? </p>
<div class="codehilite"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">expensive_const</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="w"></span>
</code></pre></div>



<a name="208468043"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208468043" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208468043">(Aug 30 2020 at 04:32)</a>:</h4>
<p>Const prop in MIR and LLVM, no guarantee though.</p>



<a name="208468421"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208468421" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Manmeet Singh <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208468421">(Aug 30 2020 at 04:45)</a>:</h4>
<p>Const prop should be guaranteed if all inputs are const and function is const</p>



<a name="208468811"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208468811" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> jknodt <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208468811">(Aug 30 2020 at 04:58)</a>:</h4>
<p>It's possible that this is not correct either, consider a case where you have a fixed address and perform some read in a const fn. Even though it is a const fn with a const argument, it may not be correct if evaluated at compile time.</p>



<a name="208468862"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208468862" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208468862">(Aug 30 2020 at 05:00)</a>:</h4>
<p>if you want to force const evaluation to happen just use <code>const</code>...</p>



<a name="208471672"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208471672" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208471672">(Aug 30 2020 at 06:41)</a>:</h4>
<p><span class="user-mention silent" data-user-id="302166">Manmeet Mann</span> <a href="#narrow/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen/near/208468421">said</a>:</p>
<blockquote>
<p>Const prop should be guaranteed if all inputs are const and function is const</p>
</blockquote>
<p>what if there's an infinite loop? what if there's a panic? what if the computation is very expensive and there's many of them?<br>
optimizations are basically never guaranteed. it would be hard to say what that even means, and it would often not even be a good idea.</p>



<a name="208630974"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208630974" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208630974">(Sep 01 2020 at 00:03)</a>:</h4>
<p>Amusingly, I actually think the inability to define a const context without a const binding is tripping people up, because people grok unsafe { } vs. unsafe fn, but const fn is the only thing there, so they assume it carries an implicit const context, and don't quite realize that is what <code>const NAME = stuff();</code> implies.</p>
<p>And I grasp that the automatic constant-folding is not exactly == CTFE but often I have to slow way down to keep track of  any difference in, er, context. I don't think people have really been messaged on it.</p>



<a name="208634134"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208634134" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208634134">(Sep 01 2020 at 00:55)</a>:</h4>
<p>If that's the case, then the recently-approved <code>const {}</code> should help that group.</p>



<a name="208636480"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208636480" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208636480">(Sep 01 2020 at 01:44)</a>:</h4>
<p>Yeah! I really do suspect not having to use binding and also realizing there's an explicit const context expression will make a lot of people go "<em>oh</em>", and probably still confusedly flail a bit at stuff they don't quite understand, but confusedly flail in the right direction.</p>



<a name="208652823"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208652823" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208652823">(Sep 01 2020 at 07:30)</a>:</h4>
<p><span class="user-mention" data-user-id="281757">@Jubilee</span> that sounds reasonable, let's hope you are right :)</p>



<a name="208652838"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208652838" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208652838">(Sep 01 2020 at 07:30)</a>:</h4>
<p>(to be clear, they have been approved for implementation. stabilization is still ways off.)</p>



<a name="208682123"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208682123" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ahmed Charles <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208682123">(Sep 01 2020 at 12:51)</a>:</h4>
<p>Will <code>const {}</code> make it so I can <code>fn foo(bar: u32) -&gt; { bar + const { some_expensive_const_function(42) } }</code> and have the expensive function run at compile time?</p>



<a name="208682264"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208682264" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208682264">(Sep 01 2020 at 12:53)</a>:</h4>
<p>yes, <code>const</code> blocks will be evaluated at compiletime</p>



<a name="208682428"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208682428" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ahmed Charles <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208682428">(Sep 01 2020 at 12:54)</a>:</h4>
<p>Does it just avoid having to add a temporary?</p>



<a name="208682727"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208682727" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208682727">(Sep 01 2020 at 12:57)</a>:</h4>
<p>const blocks are mostly identical to </p>
<div class="codehilite"><pre><span></span><code>fn foo(bar: u32) -&gt; {
    const __INTERAL: ty = some_expensive_const_function(42);
    bar + __INTERNAL
}
</code></pre></div>



<a name="208682802"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208682802" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208682802">(Sep 01 2020 at 12:58)</a>:</h4>
<p>so they mostly result in cleaner code</p>



<a name="208682816"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208682816" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208682816">(Sep 01 2020 at 12:58)</a>:</h4>
<p>not sure if they allow for anything which was previously not possible</p>



<a name="208682822"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208682822" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ahmed Charles <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208682822">(Sep 01 2020 at 12:58)</a>:</h4>
<p>Well, that without the const block, right?</p>



<a name="208682938"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208682938" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ahmed Charles <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208682938">(Sep 01 2020 at 12:59)</a>:</h4>
<p>I like recursive definitions as much as everyone else, though!</p>



<a name="208720720"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208720720" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208720720">(Sep 01 2020 at 17:30)</a>:</h4>
<p>I think they will eventually support a bit more, by being able to use generic parameters that are in-scope since they're an expression and not an item (like how a closure can use generic parameters from its enclosing function but a function cannot).  That needs some more type system implementation work though (some of the same stuff as non-min const generics) so it won't support that in the first part.</p>



<a name="208755842"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208755842" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Santiago Pastorino <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208755842">(Sep 01 2020 at 22:15)</a>:</h4>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> <a href="#narrow/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen/near/208471672">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="302166">Manmeet Mann</span> <a href="#narrow/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen/near/208468421">said</a>:</p>
<blockquote>
<p>Const prop should be guaranteed if all inputs are const and function is const</p>
</blockquote>
<p>what if there's an infinite loop? what if there's a panic? what if the computation is very expensive and there's many of them?<br>
optimizations are basically never guaranteed. it would be hard to say what that even means, and it would often not even be a good idea.</p>
</blockquote>
<p>maybe one good example is fib(100000000) implemented recursively :P</p>



<a name="208760436"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208760436" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208760436">(Sep 01 2020 at 23:09)</a>:</h4>
<p>Yeah, you'd be compiling for years...</p>



<a name="208761657"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208761657" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208761657">(Sep 01 2020 at 23:24)</a>:</h4>
<p>Which I'd say is better than forcing the end user to run it for years...</p>



<a name="208774880"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208774880" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Monadic Cat <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208774880">(Sep 02 2020 at 03:04)</a>:</h4>
<p>I wouldn't say that. The end user might be a super computer built for the purpose.</p>



<a name="208775005"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208775005" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208775005">(Sep 02 2020 at 03:06)</a>:</h4>
<p>I have an idea! Tell the user if their computation is expensive! (Just kidding, I think that's the halting problem.)</p>



<a name="208776120"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208776120" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Monadic Cat <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208776120">(Sep 02 2020 at 03:34)</a>:</h4>
<p>Making const evaluation guaranteed where all inputs to a const fn are constants would lead to me writing increasingly cursed things to try and suppress that behavior.</p>



<a name="208800556"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208800556" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208800556">(Sep 02 2020 at 09:45)</a>:</h4>
<p><span class="user-mention silent" data-user-id="336395">Ahmed Charles</span> <a href="#narrow/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen/near/208682428">said</a>:</p>
<blockquote>
<p>Does it just avoid having to add a temporary?</p>
</blockquote>
<p>yes</p>



<a name="208800638"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208800638" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208800638">(Sep 02 2020 at 09:46)</a>:</h4>
<p><span class="user-mention silent" data-user-id="334100">Monadic Cat</span> <a href="#narrow/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen/near/208776120">said</a>:</p>
<blockquote>
<p>Making const evaluation guaranteed where all inputs to a const fn are constants would lead to me writing increasingly cursed things to try and suppress that behavior.</p>
</blockquote>
<p>could you elaborate? when would you not want that to happen (assuming we can do it without excessive compile times)?</p>



<a name="208831871"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208831871" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Monadic Cat <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208831871">(Sep 02 2020 at 14:27)</a>:</h4>
<p>I mean, if you could do it without excessive compile times, then I suppose there's nothing. (I am assuming that such evaluation would never be allowed to change the observable behavior of a program at runtime, except timing, that is. Since platform specific stuff and IO are disallowed in const contexts, I believe that to be the case.)</p>
<p>It's just that I don't really see how you'd make that guarantee without possibly incurring a massive compile time penalty on someone without their code changing at all.<br>
(...is that a Crater run waiting to happen?)</p>
<p>Okay, having done a bit more looking, there is already a mechanism for dealing with this: <a href="https://github.com/rust-lang/rust/issues/67217">https://github.com/rust-lang/rust/issues/67217</a><br>
In the case of implicit const evaluation, would you make it so it silently gives up when that limit is reached?<br>
<del><em>Or maybe run it to the limit and store the end state so the running program can pick up where const eval left off...</em></del></p>
<p>On further reflection, the only thing I've written that feels like it'd be afflicted by the excessive compile times described above was this thing at involved <code>include_bytes!</code>-ing a giant block of data and doing stuff with it at program startup- since Miri is the big slow, I figured that I'd still prefer that to happen at runtime than in const evaluation.</p>
<p>I've ended up feeling more ambivalent about this than I expected.</p>



<a name="208850547"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Common%20misconception%3A%20%60const%20fn%60%20and%20its%20effect%20on%20codegen/near/208850547" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Common.20misconception.3A.20.60const.20fn.60.20and.20its.20effect.20on.20codegen.html#208850547">(Sep 02 2020 at 16:23)</a>:</h4>
<blockquote>
<p>(I am assuming that such evaluation would never be allowed to change the observable behavior of a program at runtime, except timing, that is. Since platform specific stuff and IO are disallowed in const contexts, I believe that to be the case.)</p>
</blockquote>
<p>That is the case for <em>all</em> optimizations, including this one.</p>
<blockquote>
<p>It's just that I don't really see how you'd make that guarantee without possibly incurring a massive compile time penalty on someone without their code changing at all.</p>
</blockquote>
<p>I think so, too, which is why I don't think we should do something like this. But I was wondering what your reasoning is.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>